﻿using INTEX.DataBase.Tools;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class YCAFS20 : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        YCAFS20Data m_Data, m_DataX;
        public DataSet setUI=new DataSet();
        public DataSet dsParamaters;
        public DataRow dwABPOS;
        public DataRow dwFSEGROE;
        public INTEXDBC INTEXDBC;
        private DataRow dwUI;
        private string fromdate = "", enddate = "";
        DataTable tableUI;
        private string flag = "";
        #endregion
        public YCAFS20()
        {
            InitializeComponent();
            this.Name = "尺码接品定义";
           
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            InitializeComponent();
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            orlReadCommand = OracleDB.CreateCommand();
            orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
            m_Data = new YCAFS20Data();
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy;
            INTEXDBC = new INTEXDBC(OracleDB, proxy);
            tableUI = new DataTable("UIData");
            tableUI.Columns.Add("MANDANT", typeof(string));
            tableUI.Columns.Add("SZGRP", typeof(string));
            tableUI.Columns.Add("SZ", typeof(string));
            tableUI.Columns.Add("DESCR", typeof(string));
            tableUI.Columns.Add("INTERFACE1", typeof(string));
            tableUI.Columns.Add("INTERFACE2", typeof(string));
        }

        private void InitDataBinding()
        {
             
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                dwUI = tableUI.NewRow(); 
                InitDataBinding();
                ex1.IsExpanded = false;
                txtSZGRP.Text = "";
                txtSZ.Text = "";
                txtDESCR.Text = "";
                txtINTERFACE.Text = "";
                flag = "";
                btnSave.IsEnabled = false; 
                btnNew.IsEnabled = false;
                btnModify.IsEnabled = false;
                btnDelete.IsEnabled = false;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); 
            }
            return;

        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    DataSet setResult = new DataSet();
                    setUI.Clear();
                    setUI.AcceptChanges();                    
                    setResult = m_Data.BuildYCAFS20Data(dwUI);
                    setUI.Merge(setResult);
                    MasterDg.DataContext = setUI.Tables["WASTAMM"];
                    btnSave.IsEnabled = false;
                    btnNew.IsEnabled = true;
                    btnModify.IsEnabled = true;
                    btnDelete.IsEnabled = true;
                }
                
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnEXCEL_Click(object sender, RoutedEventArgs e)
        {
            m_DataX = new YCAFS20Data(dicParamater);
            m_DataX.exAgent._app.Visible = false;
            m_DataX.OracleDB = this.OracleDB;
            m_DataX.proxy = this.proxy;
            YCAFS20Report m_rpt = new YCAFS20Report(dicParamater);
            m_rpt.dsSource = setUI;
            m_rpt.PrintType = "OpenExcel";
            m_rpt.exAgent = m_DataX.exAgent;
            m_rpt.WriteExcelReport("EXCEL");
        }

        
        private void btnNew_Click(object sender, RoutedEventArgs e)
        {
            
                ex1.IsExpanded = true;
                txtSZGRP.Text = "";
                txtSZ.Text = "";
                txtDESCR.Text = "";
                txtINTERFACE.Text = "";
                txtSZGRP.IsReadOnly = false;
                txtSZ.IsReadOnly = false;
                txtDESCR.IsReadOnly = false;
                txtINTERFACE.IsReadOnly = false;
                btnQuery.IsEnabled = false;
                btnNew.IsEnabled = false;
                btnModify.IsEnabled = false;
                btnDelete.IsEnabled = false;
                btnSave.IsEnabled = true;
                flag = "NEW";
        }

        private void btnModify_Click(object sender, RoutedEventArgs e)
        {
            
                ex1.IsExpanded = true;
                txtSZGRP.IsReadOnly = true;
                txtSZ.IsReadOnly = true;
                txtDESCR.IsReadOnly = false;
                txtINTERFACE.IsReadOnly = false;
                btnQuery.IsEnabled = false;
                btnNew.IsEnabled = false;
                btnModify.IsEnabled = false;
                btnDelete.IsEnabled = false;
                btnSave.IsEnabled = true;
                flag = "MODIFY";
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
           
                ex1.IsExpanded = true;
                btnQuery.IsEnabled = false;
                btnNew.IsEnabled = false;
                btnModify.IsEnabled = false;
                btnDelete.IsEnabled = false;
                btnSave.IsEnabled = true;
                flag = "DELETE";
           
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {

                string SZGRP = m_Data.Left(txtSZGRP.Text.ToString().Trim() + "   ", 3);
                string SZ = m_Data.Left(txtSZ.Text.ToString().Trim() + "       ", 7);
                string MANDANT = "00";
                string DESCR = txtDESCR.Text.ToString().Trim();
                string INTERFACE1 = txtINTERFACE.Text.ToString().Trim();
                string INTERFACE2 = "";

                dwUI["MANDANT"] = MANDANT;
                dwUI["SZGRP"] = SZGRP;
                dwUI["SZ"] = SZ;
                dwUI["DESCR"] = DESCR;
                dwUI["INTERFACE1"] = INTERFACE1;
                dwUI["INTERFACE2"] = INTERFACE2;

                if (flag == "NEW")
                {
                    m_Data.INSERTDATA(dwUI);
                }
                else if (flag == "MODIFY")
                {
                    m_Data.UPDATEDATA(dwUI);
                }
                else if (flag == "DELETE")
                {
                    m_Data.DELETEDATA(dwUI);
                }
                ex1.IsExpanded = false;
                txtSZGRP.Text = "";
                txtSZ.Text = "";
                txtDESCR.Text = "";
                txtINTERFACE.Text = "";
                txtSZGRP.IsReadOnly = true;
                txtSZ.IsReadOnly = true;
                txtDESCR.IsReadOnly = true;
                txtINTERFACE.IsReadOnly = true;
                btnQuery.IsEnabled = true;
                flag = "";
                btnQuery_Click(null, null);

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        private void MasterDg_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            try
            {
                if (flag == "")
                {
                    if (e.AddedItems.Count == 0)
                        return;
                    DataRowView drv = e.AddedItems[0] as DataRowView;
                    txtSZGRP.Text = drv.Row["SZGRP"].ToString().Trim();
                    txtSZ.Text = drv.Row["SZ"].ToString().Trim();
                    txtDESCR.Text = drv.Row["DESCR"].ToString().Trim();
                    txtINTERFACE.Text = drv.Row["INTERFACE1"].ToString().Trim();
                }
                else
                {
                    MessageBox.Show("请先确认保存数据，再选择其它记录!");
                    return;
                }


            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;
        }

    }
}
